package plateforme.controlleur;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import plateforme.connexion.ConnectionManager;

import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;

public class ControlleurChercherPersonne  extends HttpServlet{
	
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ServletConfig config;
	//Setting JSP page

	  String page="resultat_recherche_utilisateur.jsp";
	  String page1="chercher_utilisateur.jsp";
	 
	  
	  
	  public void init(ServletConfig config) throws ServletException{

	 this.config=config;

	 }

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {
		
		List<String>personlist =new ArrayList<String>();
		//String nom=request.getParameter("nom");
		String role=request.getParameter("role");
		String specialite=request.getParameter("specialite");
		
		
		 String i=null;
		 String n=null;
		 String p = null;
		 String r=null;
		 String f=null;
		 String l=null;
		 String pa=null;
		 String em=null;
		 if(role.length()!=0){
		try {
			

			
			System.out.println(role+specialite);
			
			
			ResultSet rs;
			Connection cn=null;
			 cn=ConnectionManager.getConnection();
			// String sql = "select * from personne  where role='"+role+"'OR specialite='"+specialite+"'" ;
			String sql = "select * from personne  where role='"+role+"'" ;
			 Statement s = (Statement) cn.createStatement();

			  s.executeQuery (sql);

				
			  rs = (ResultSet) s.getResultSet();
			
			  while (rs.next ()){
				  
				  
				
				  //Add records into data list

				  personlist.add(rs.getString(1));

				  personlist.add(rs.getString(2));

				  personlist.add(rs.getString(3));
				  
				  personlist.add(rs.getString(4));
				  personlist.add(rs.getString(5));
				  personlist.add(rs.getString(6));
				  personlist.add(rs.getString(7));
				  personlist.add(rs.getString(8));
			  } 
			 
			 
			  Iterator <String>itr;
			  for (itr=personlist.iterator();itr.hasNext(); )
			  {
				 i=itr.next(); 
				 n=itr.next();
				 p=itr.next();
				  r=itr.next();
				  f=itr.next();
				  l=itr.next();
				  pa=itr.next();
				  System.out.println(i+"+"+n+"+"+p+"+"+r+""+f+""+l+"+"+em); 
			  }
			  
			  
			
			  
			  rs.close ();

			  s.close ();

			 
			 
                  
			  //Disptching request
			 

			  
				// System.out.println(id+"+"+nom+"+"+prenom+"+"+role+""+fonction+""+login+"+"+password); 
				 //System.out.println(identifiant); 
				  
			  
		 
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		
		//Disptching request
        
  	  
		 request.setAttribute("data",personlist);

		  //Disptching request
		  RequestDispatcher dispatcher = request.getRequestDispatcher(page);

		  if (dispatcher != null){

		  dispatcher.forward(request, response);

		  

		  }
		 else{
			 
			 RequestDispatcher dispatcher1 = request.getRequestDispatcher(page1);

			  if (dispatcher1 != null){

			  dispatcher1.forward(request, response);

			  } 
			 }
	}

}
}